﻿//-----------------------------------------------------------------
//
// @(#)$Id: UpdateOrder.aspx.cs,v 1.0 2013/12/13 17:57:51 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:57:51 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal;
using ebay.Dal.CenterDB_BasTableAdapters;

public partial class UI_Orders_UpdateOrder : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        /*
        T_ExpressShippingMethodsTableAdapter eAdapter = new T_ExpressShippingMethodsTableAdapter();
        ddlDeliveryId.DataSource = eAdapter.GetData();
        ddlDeliveryId.DataValueField = "Id";
        ddlDeliveryId.DataTextField = "Delivery";
        ddlDeliveryId.DataBind();
        ddlDeliveryId.Items.Insert(0, new ListItem("<--请选择-->", "0"));*/

        T_ExpressShippingMethodsTableAdapter eAdapter = new T_ExpressShippingMethodsTableAdapter();
        ddlpostageservice.DataSource = eAdapter.GetData();
        ddlpostageservice.DataValueField = "EbayShippingMethods";
        ddlpostageservice.DataTextField = "EbayShippingMethods";
        ddlpostageservice.DataBind();
        ddlpostageservice.Items.Insert(0, new ListItem("<--请选择-->", ""));

        //重发原因
        T_ReBuilderReasonTableAdapter rAdapter = new T_ReBuilderReasonTableAdapter();
        ddlReBuilderReson.DataSource = rAdapter.GetData();
        ddlReBuilderReson.DataValueField = "ReBuilderReason";
        ddlReBuilderReson.DataTextField = "ReBuilderReason";
        ddlReBuilderReson.DataBind();
        ddlReBuilderReson.Items.Insert(0, new ListItem("<--请选择-->", "0"));
    }
    static string ebayUserId="";
    static string orderLineItemId = "";
    static string orderId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ebayUserId = Request["id"];
            orderLineItemId = Request["id2"];
            orderId = Request["id3"];
            if (!string.IsNullOrEmpty(ebayUserId) && !string.IsNullOrEmpty(orderLineItemId) && !string.IsNullOrEmpty(orderId))
            {
                string cmdText = "Select ebayUserId,orderId,BuyerId,BuyerName,AddressLine1,AddressLine2,City,County,PostalCode,Country,ContactPhoneNumber,postageservice,ReBuilderReson,Memo,[OrderRetentionReson],[BeiZhu] From Orders Where OrderLineItemId = @OrderLineItemId And EbayUserId = @EBayUserId";
                //修改：2012-8-28
                //string cmdText = "Select UserId,BuyerFullName,AddressLine1,AddressLine2,City,County,PostalCode,Country,ContactPhoneNumber,DeliveryId,ReBuilderReson,Memo From Orders Where OrderLineItemId = @OrderLineItemId And EbayUserId = @EBayUserId";
                SqlParameter[] p = { 
                               new SqlParameter("@EBayUserId",SqlDbType.VarChar),
                               new SqlParameter("@OrderLineItemId",SqlDbType.VarChar)
                               };
                p[0].Value = ebayUserId;
                p[1].Value = orderLineItemId;
                DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, p);
                if (table.Rows.Count > 0)
                {
                    DataRow r = table.Rows[0];
                    foreach (Control txtControl in this.form2.Controls)
                    {
                        if (txtControl is HtmlInputText)
                        {
                            HtmlInputText txtBox = (HtmlInputText)txtControl;
                            string colName = txtBox.ID.Substring(3, txtBox.ID.Length - 3);
                            string val = r[colName].ToString();
                            if (table.Columns[colName].DataType.Name == "DateTime")
                            {
                                DateTime valDate = DateTime.Parse(val);
                                DateTime defaultDateTime = new DateTime(1900, 1, 1);
                                if (valDate == defaultDateTime)
                                {
                                    val = "";
                                }
                            }                           
                            txtBox.Value = val;
                        }
                        else if (txtControl is HtmlSelect)
                        {
                            HtmlSelect ddList = (HtmlSelect)txtControl;
                            string colName = ddList.ID.Substring(3, ddList.ID.Length - 3);
                            ddList.Value = r[colName].ToString();

                        }
                        else if (txtControl is HtmlTextArea)
                        {
                            HtmlTextArea txtarea = (HtmlTextArea)txtControl;
                            string colName = txtarea.ID.Substring(3, txtarea.ID.Length - 3);
                            txtarea.Value = r[colName].ToString();
                        }
                    }
                }
            }
        }

    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        string MebayUserId = txtebayUserId.Value.ToString();
        string MorderId = txtorderId.Value.ToString();
        string PostageService = ddlpostageservice.Value.ToString();
        if (string.IsNullOrEmpty(MebayUserId) || string.IsNullOrEmpty(orderLineItemId) || string.IsNullOrEmpty(MorderId))
        {
            WebUtility.ShowMsg("参数错误", this);
            return;
        }
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        if (string.IsNullOrEmpty(PostageService))
        {
            WebUtility.ShowMsg("请选择发货方式！", this);
            return;
        }
        string userId = Session["UserId"].ToString();        
        Orders o = new Orders();
        try
        {
            /*
            o.UpdateOrder(txtBuyerId.Value, txtBuyerName.Value, txtAddressLine1.Value, txtAddressLine2.Value, txtCity.Value, txtCounty.Value,
                txtPostalCode.Value, txtCountry.Value, txtContactPhoneNumber.Value, userId, ebayUserId, orderId,
                Convert.ToInt32(ddlDeliveryId.Value), ddlReBuilderReson.Value);
             * */

            o.UpdateOrder(txtBuyerId.Value, txtBuyerName.Value, txtAddressLine1.Value, txtAddressLine2.Value, txtCity.Value, txtCounty.Value,
    txtPostalCode.Value, txtCountry.Value, txtContactPhoneNumber.Value, userId, MebayUserId, MorderId,
    ddlpostageservice.Value, ddlReBuilderReson.Value, txtOrderRetentionReson.Value, txtBeiZhu.Value);
            //Log.WriteLog("ebayUserId:" + ebayUserId + " " + "orderId:" + orderId + " " + "AddressLine1:" + txtAddressLine1.Value + " " + "AddressLine2" + txtAddressLine2.Value);
            Response.Write("<script>window.returnValue = true;window.close();</script>");
        }
        catch {
            throw;
        }
    }
}
